package com.harmontronics.hanma.paas.paas.utils;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @author ldb
 */
public class RequestUtils {

    public static final HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if(servletRequestAttributes == null){
            return null;
        }
        return  servletRequestAttributes.getRequest();
    }

    /**
     * 获取租户访问的域名
     * @return
     */
    public static String getDomainSuffix() {
        HttpServletRequest request = getRequest();
        String domainSuffix = StringUtils.trimWhitespace(request.getHeader("Domain-Suffix"));
        return StringUtils.isEmpty(domainSuffix) ? StringUtils.trimWhitespace(request.getParameter("Domain-Suffix")) : domainSuffix;
    }

    public static String getToken() {
        HttpServletRequest request = getRequest();
        if(request == null){
            return null;
        }
        String token = StringUtils.trimWhitespace(request.getHeader("Access-Token"));
        return StringUtils.isEmpty(token) ? StringUtils.trimWhitespace(request.getParameter("Access-Token")) : token;
    }

    public static Boolean getPreview(){
        HttpServletRequest request = getRequest();
        if(request == null){
            return false;
        }

        String appPreview = StringUtils.trimWhitespace(request.getHeader("App-Preview"));
        if(appPreview == null){
            return false;
        }
        return appPreview.equals("true");
    }




    /**
     * 获取app_unique
     * @return
     */
    public static String getAppUnique(){
        HttpServletRequest request = getRequest();
        if(request == null){
            return null;
        }
        String appUnique = StringUtils.trimWhitespace(request.getHeader("App-Unique"));
        return StringUtils.isEmpty(appUnique) ? StringUtils.trimWhitespace(request.getParameter("App-Unique")) : appUnique;
    }

    public static String localeMessage(String i18nKey, Object... args) {
        HttpServletRequest request = getRequest();
        Locale locale = LocaleContextHolder.getLocale();
        String localStr = request.getHeader("locale");
        if(StringUtil.isEmpty(localStr)){
            localStr = "zh-CN";
        }
        String[] arr = localStr.split("-");
        if(arr.length == 2) {
            locale = new Locale(arr[0], arr[1]);
        }else if(arr.length == 1){
            locale = new Locale(arr[0]);
        }

        WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
        return wac.getMessage(i18nKey, args, locale);
    }
}
